/*
 * Copyright (c) 2011 Junichiro Okuno
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Junichiro Okuno.
 */

package tcp_net.mail.smtp.commands;

import tcp_net.mail.smtp.commands.debug.DebugInfo;
import tcp_net.ReplyMessage;
import java.util.ArrayList;
import java.util.List;

/**
	This class provides a reply command at SMTP level
	@author J. Okuno
*/
public class SMTPReplyMessage extends ReplyMessage
{
	/**
		true if it is an acceptable command otherwise false
	*/
	boolean okay;
	
	/**
		An SMTP command
	*/
	SMTPCommand command;
	
	/**
		Init the class
		@param ok a positive reply (true), a negative reply (false)
		@param c an smtp command
	*/
	public SMTPReplyMessage(boolean ok, SMTPCommand c) 
	{
		okay = ok;
		command = c;
	}
	
	/**
		Init the class
		@param c an smtp command
	*/
	public SMTPReplyMessage(SMTPCommand c) 
	{
		this(true, c);
	}
	
	/**
		This method executes a defined command
		@return a list of reply messages in a string format
	*/
	public List<String> execute() 
	{ 
		List<String> reply_msgs = new ArrayList<String>();
		String response = "";
		
		if(okay)
		{
			//server accepts a request command
			if(command instanceof EHLO)
			{
				EHLO ehlo = (EHLO)command;
				reply_msgs.add("250-"+ehlo.getDomain());
				reply_msgs.add("250-PIPELINING");
				reply_msgs.add("250-ETRN");
				reply_msgs.add("250 8BITMIME");
				
				return reply_msgs; 
			}
			
			else if(command instanceof DATA)
			{
				reply_msgs.add(get354());
				return reply_msgs; 
			}
			
			else if(command instanceof MAIL)
			{
				reply_msgs.add(get250());
				return reply_msgs; 
			}
			
			else if(command instanceof RCPT)
			{
				reply_msgs.add(get250());
				return reply_msgs; 
			}
			
			else if(command instanceof HELO)
			{
				reply_msgs.add("250 Hello");
				return reply_msgs; 
			}
			
			else if(command instanceof HELP)
			{
				reply_msgs.add("214 Avaliable commands:");
				reply_msgs.add("214 HELO EHLO MAIL RCPT DATA");
				reply_msgs.add("214 RSET QUIT HELP");
				reply_msgs.add("214 End of HELP info");
				return reply_msgs; 
			}
			
			else if(command instanceof NOOP || command instanceof RSET)
			{
				reply_msgs.add(get250());
				return reply_msgs; 
			}
			
			else if(command instanceof QUIT)
			{
				QUIT quit = (QUIT)command;
				reply_msgs.add(get221(quit.getDomain()));
				return reply_msgs; 
			}
			
			//Unrecog. commands
			else
			{
				reply_msgs.add(get500());
				return reply_msgs; 
			}
		}
		
		//error cases
		else
		{
			//Does not support these commands
			if(command instanceof EXPN || command instanceof VRFY)
			{
				reply_msgs.add(get550());
				return reply_msgs; 
			}
		}
		
		return reply_msgs; 
	}
	
	/**
		SMTP Server reply code
	*/
	private String get221() { return "211 System status, or system help reply"; }
	
	/**
		SMTP Server reply code
	*/
	private String get214() { return "214 Help message"; }
	
	/**
		SMTP Server reply code
		@param domain a domain name
	*/
	private String get220(String domain) { return "220 <"+ domain +"> Service ready"; }
	
	/**
		SMTP Server reply code
		@param domain a domain name
	*/
	private String get221(String domain) { return "221 <"+ domain +"> Service closing transmission channel"; }
	
	/**
		SMTP Server reply code
		@param domain a domain name
	*/
	private String get250() { return "250 Requested mail action okay, completed"; }
	
	/**
		SMTP Server reply code
		@param domain a domain name
	*/
	private String get251(String forward_path) { return "251 User not local; will forward to <"+ forward_path+">"; }
	
	/**
		SMTP Server reply code
	*/
	private String get252() { return "252 Cannot VRFY user, but will accept message and attempt delivery";}
	
	
	/**
		SMTP Server reply code
	*/
	private String get354() { return "354 Start mail input; end with <CRLF><CRLF>"; }
	
	/**
		SMTP Server reply code
		@param domain a domain name
	*/
	private String get421(String domain) { return "421 <"+ domain +"> Service mail input; end with <CRLF>.<CRLF>"; }
	
	/**
		SMTP Server reply code
	*/
	private String get450() { return "450 Requested mail action not taken; mailbox unavailable"; }
	
	/**
		SMTP Server reply code
	*/
	private String get451() { return "451 Requested action aborted: error in processing"; }
	
	/**
		SMTP Server reply code
	*/
	private String get452() { return "452 Requested action not taken: insufficient system storage"; }
	
	
	/**
		SMTP Server reply code
	*/
	private String get500() { return "500 Syntax error, commend unrecognized"; }
	
	/**
		SMTP Server reply code
	*/
	private String get501() { return "501 Syntax error in parameters or arguments"; }
	
	/**
		SMTP Server reply code
	*/
	private String get502() { return "502 Command not implemented"; }
	
	/**
		SMTP Server reply code
	*/
	private String get503() { return "503 Bad sequence of commands"; }
	
	/**
		SMTP Server reply code
	*/
	private String get504() { return "504 Command parameter not implemented"; }
	
	/**
		SMTP Server reply code
	*/
	private String get550() { return "550 Requested action not taken; mailbox unavaliable"; }
	
	/**
		SMTP Server reply code
	*/
	private String get551() { return "551 User not local"; }
	
	/**
		SMTP Server reply code
	*/
	private String get552() { return "552 Requested mail action aborted; exceded storage allocation"; }
	
	/**
		SMTP Server reply code
	*/
	private String get553() { return "553 Requested action not taken; mailbox name not allowed"; }
	
	/**
		SMTP Server reply code
	*/
	private String get554() { return "554 Transaction failed"; } 
}